package org.example.service.Impl;

import org.example.exception.BusinessException;
import org.example.mapper.FoodMapper;
import org.example.pojo.Food;
import org.example.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {

    @Autowired
    private FoodMapper foodMapper;

    /*
    * 查询所有食物
    * */
    @Override
    public List<Food> getAllFoods() {
        return foodMapper.findAll();
    }


    /*
    * 添加食物
    * */
    @Override
    public void addFood(Food food) {
        if (food.getName() == null || food.getName().isEmpty()) {
            throw new BusinessException("食品名称不能为空");
        }
        if (food.getPrice() == null || food.getPrice().compareTo(BigDecimal.ZERO) <= 0) {
            throw new BusinessException("价格必须大于0");
        }
        foodMapper.insert(food);
    }

    /*
    * 根据ID查询食物
    * */
    public Food getFoodById(Integer id) {
        Food food = foodMapper.findById(id);
        if (food == null) {
            throw new BusinessException("食品不存在");
        }
        return food;
    }

    /*
    * 删除食物
    * */
    public void deleteFood(Integer id) {
        foodMapper.delete(id);
    }

    /*
    * 更新食物信息
    * */
    @Override
    public void updateFood(Integer id, Food food) {
        foodMapper.update(food);
    }

}

